package hn.cch.grammar.clone;

public class CloneDemo implements Cloneable {

    private int id;
    private Integer no;
    private DeepClone deepClone;

    public CloneDemo() {
    }

    public CloneDemo(int id, Integer no) {
        this.id = id;
        this.no = no;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Integer getNo() {
        return no;
    }

    public void setNo(Integer no) {
        this.no = no;
    }

    public DeepClone getDeepClone() {
        return deepClone;
    }

    public void setDeepClone(DeepClone deepClone) {
        this.deepClone = deepClone;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        CloneDemo clone = (CloneDemo) super.clone();
        clone.setDeepClone((DeepClone) this.deepClone.clone());// 深度拷贝
        return clone;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        CloneDemo c1 = new CloneDemo(1, 1);
        c1.setDeepClone(new DeepClone(1));
        CloneDemo c2 = (CloneDemo) c1.clone();
        c2.setId(2);
        c2.setNo(2);
        c2.getDeepClone().setField(2);
        System.out.println();
    }
}
